import React, {useContext} from 'react'
import './index.css'
import { nanoid } from 'nanoid'
import { Context } from '../../App'

function Header(props){
    const {dispatch} = useContext(Context)
    function handleInput(e){
        if(e.keyCode === 13){
            dispatch({type:'addTodo',todo:{id:nanoid(),name:e.target.value,done:false}})
            e.target.value = ''
        }
    }
    return(
        <div className="todo-header">
            <input type="text" placeholder="请输入你的任务名称，按回车键确认" onKeyDown={handleInput}/>
        </div>
    )
}

export default Header